// pages/break/break.js
import { http } from '../../utils/requestUtil'
// import { formatTime } from '../../utils/util'

Page({
  data: {
    noteList: [],       // 便签列表数据
    pageNum: 1,        // 当前页码
    pageSize: 100,       // 每页数量
    hasMore: true,      // 是否有更多数据
    loading: false,     // 加载状态
    filterIndex: 0,     // 筛选索引
    filterOptions: ['全部', '重要', '普通', '不重要'] // 筛选选项
  },
  async restart(e){
wx.showModal({
      title: '提示',
      content: '重新启动该标签',
      success: async(res) => {
        if (res.confirm) {
          const currentId = e.currentTarget.dataset.id
          const res = await http.put("/memo/note",{noteId:currentId,delFlag:'0'})
          
          this.loadData()
        } else if (res.cancel) {
          console.log('用户点击了取消')
        }
      }
    })
  },

  // 加载数据
  async loadData() {
    // if (!this.data.hasMore || this.data.loading) return
    
    this.setData({ loading: true })
    this.setData({
      noteList:[]
    })
    try {
      const params = {
        delFlag: '2',
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize
      }
      
      // 根据筛选条件添加参数
      if (this.data.filterIndex > 0) {
        params.importance = ['0', '1', '3'][this.data.filterIndex - 1]
      }
      
      const res = await http.get("/memo/note/userlist", params)
      
      if (res.code === 200) {
        const data = res.rows || []
        // const noteList = this.data.pageNum === 1 ? data : [...this.data.noteList, ...data]
        const noteList = res.rows
        this.setData({
          noteList:noteList,
          hasMore: data.length >= this.data.pageSize,
          pageNum: this.data.pageNum + 1,
          loading: false
        })
      }
    } catch (error) {
      console.error('加载数据失败:', error)
      wx.showToast({
        title: '加载失败，请重试',
        icon: 'none'
      })
      this.setData({ loading: false })
    }
  },

  // 格式化时间
  formatTime(time) {
    // return formatTime(new Date(time), 'yyyy-MM-dd hh:mm')
    return "2022-2-2"
  },

  // 筛选条件变化
  onFilterChange(e) {
    const index = e.detail.value
    this.setData({
      filterIndex: index,
      pageNum: 1,
      hasMore: true,
      noteList: []
    }, () => {
      this.loadData()
    })
  },

  // 滚动到底部加载更多
  onReachBottom() {
    // this.loadData()
  },

  // 页面加载
  // onLoad(options) {
  //   this.loadData()
  // },
  onShow(){
    this.loadData()
  }
})